$(document).ready(function() {
	function formatearCuestionario() {
		$("label[class^='required']").each(function() {
			this.innerHTML = this.textContent;
		});

		$("div[id^='form_question_']").each(function() {
			var result = ""
			var idx = 1;
			$('#' + this.id).children().each(function() {
				idx++;
				result += this.outerHTML;
				if (idx % 2) {
					result += '<br/>'
				}
			});
			$('#' + this.id).html(result);
		});
	}

	formatearCuestionario();
	

	var count = $("#form_time").val();

	timer = $.timer(function() {
		count--;
		formatCount = formatTimeMMSS(count);
		$('#timer').html(formatCount);
		$("#form_time").val(count);
		if (count === 0) {
			this.stop();
			$( "#formCuestionario" ).submit();
		}
	});

	timer.set( {
		time : 1000,
		autostart : true
	});

	// Common functions
	function pad(number, length) {
		var str = '' + number;
		while (str.length < length) {
			str = '0' + str;
		}
		return str;
	}

	function formatTimeMMSS(time) {
		var min = parseInt(time / 60), sec = parseInt(time) - (min * 60);
		return (min > 0 ? pad(min, 2) : "00") + ":" + pad(sec, 2);
	}

	$(".linkImg").click(function(){
	  var path = $("#pathSimbolos").val();
	  var target = $(this);
	  var idElement = this.id;
	  var nPreg = idElement.substring(idElement.lastIndexOf("_p") + 2, idElement.lastIndexOf("."));
	  var title = 'Simbolo Pregunta ' + nPreg;

	  $("#simboloDisplay").attr('src', path + '/' +  $(this).attr('id'));
	  $( "#modalSimbolo" ).dialog('option', 'title', title);
	  $( "#modalSimbolo" ).dialog('close');
	  $( "#modalSimbolo" ).dialog('open');
	  $("#modalSimbolo").dialog("widget").position({
	    my: 'left+25',
	    at: 'right',
	    of: target
	  });
	  
	  event.preventDefault();
	});

	$( "#modalSimbolo" ).dialog({
			autoOpen: false,
			draggable: true,
			modal: false,
			buttons: [
				{
					text: "Ok",
					click: function() {
						$( this ).dialog( "close" );
					}
				}
			]
		});
});
